package com.shuda.business.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuda.business.user.entity.PermissionEntity;
import com.shuda.business.user.service.PermissionService;
import com.shuda.common.dto.PermissionDTO;
import com.shuda.common.result.Result;
import com.shuda.common.vo.PermissionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/api/permission")
@RequiredArgsConstructor
@Tag(name = "权限管理", description = "权限管理相关接口")
public class PermissionController {

    private final PermissionService permissionService;

    /**
     * 获取权限列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取权限列表")
    @PreAuthorize("hasAuthority('permission:view')")
    public Result<IPage<PermissionEntity>> getPermissionList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "权限名称") @RequestParam(required = false) String permissionName,
            @Parameter(description = "权限编码") @RequestParam(required = false) String permissionCode,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(permissionService.getPermissionList(pageNum, pageSize, permissionName, permissionCode, status));
    }

    /**
     * 获取权限树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取权限树")
    @PreAuthorize("hasAuthority('permission:view')")
    public Result<List<PermissionVO>> getPermissionTree() {
        return Result.success(permissionService.getPermissionTree());
    }

    /**
     * 获取权限详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取权限详情")
    @PreAuthorize("hasAuthority('permission:view')")
    public Result<PermissionEntity> getPermissionById(@Parameter(description = "权限ID") @PathVariable Long id) {
        return Result.success(permissionService.getPermissionById(id));
    }

    /**
     * 创建权限
     */
    @PostMapping
    @Operation(summary = "创建权限")
    @PreAuthorize("hasAuthority('permission:create')")
    public Result<Boolean> createPermission(@RequestBody PermissionDTO permissionDTO) {
        return Result.success(permissionService.createPermission(permissionDTO));
    }

    /**
     * 更新权限
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新权限")
    @PreAuthorize("hasAuthority('permission:update')")
    public Result<Boolean> updatePermission(
            @Parameter(description = "权限ID") @PathVariable Long id,
            @RequestBody PermissionDTO permissionDTO) {
        return Result.success(permissionService.updatePermission(id, permissionDTO));
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限")
    @PreAuthorize("hasAuthority('permission:delete')")
    public Result<Boolean> deletePermission(@Parameter(description = "权限ID") @PathVariable Long id) {
        return Result.success(permissionService.deletePermission(id));
    }

    /**
     * 更新权限状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新权限状态")
    @PreAuthorize("hasAuthority('permission:update')")
    public Result<Boolean> updatePermissionStatus(
            @Parameter(description = "权限ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        return Result.success(permissionService.updatePermissionStatus(id, status));
    }

    /**
     * 获取权限选项列表
     */
    @GetMapping("/options")
    @Operation(summary = "获取权限选项列表")
    @PreAuthorize("hasAuthority('permission:view')")
    public Result<List<PermissionVO>> getPermissionOptions() {
        return Result.success(permissionService.getPermissionOptions());
    }
}